package com.qiaofang.collectmanage.analysis;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created with IDEA
 * 此类方法值只允许，新增，不允许修改 ，修改需要review
 *
 * @author:ZJ.J
 * @Date:2018/6/1
 * @Time:14:28
 */
public class ReturnValueHandle {

    /**
     * 根据方法名称执行ReturnElement对应的方法
     *
     * @param elementValue 解析后的字符串
     * @param args         args[0]方法名称
     * @return 返回 对解析后的字符串处理得到采集数据
     */
    public static String executeMethodByMethodName(String elementValue,  JSONObject jsonObject,String... args) throws Exception{
        String methodName = args[0];
        for (ReturnValueHandle.flow t : ReturnValueHandle.flow.values()) {
            if (StringUtils.isNotBlank(methodName) && methodName.trim().equals(t.getMethodName())) {
                return t.handle(elementValue,jsonObject, args);
            }
        }
        return null;
    }

    /**
     * 定义枚举类
     */
    enum flow {
        //给枚举自定义方法
        SPLITANDGET("splitAndGet") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.splitAndGet(eleValue, jsonObject,  args);
            }
        },
        TIME("time") {
            @Override
            public String handle(String eleValue,JSONObject  jsonObject , String... args) {
                return ReturnValue.time(eleValue, jsonObject,  args);
            }
        },
        CONTAINS("contains") {
            @Override
            public String handle(String eleValue,JSONObject  jsonObject , String... args) {
                return ReturnValue.contains(eleValue, jsonObject,  args);
            }
        },
        REPLACE("replace") {
            @Override
            public String handle(String eleValue,JSONObject  jsonObject , String... args) {
                return ReturnValue.replace(eleValue,  jsonObject, args);
            }
        },
        GETNUM("getNum") {
            @Override
            public String handle(String eleValue,JSONObject  jsonObject , String... args) {
                return ReturnValue.getNum(eleValue, jsonObject,  args);
            }
        },
        SPLITANDFILL("splitAndFill") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.splitAndFill(eleValue, jsonObject,  args);
            }
        }
        ,
        FILL("fill") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.fill(eleValue,  jsonObject, args);
            }
        } ,
        GETVALUEBYJSONKEY("getValueByJsonKey") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.getValueByJsonKey(eleValue,  jsonObject, args);
            }
        } ,
        IFELSE("ifElse") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.ifElse(eleValue, jsonObject,  args);
            }
        },
        ADDSITE("addSite") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) throws Exception {
                return ReturnValue.addSite(eleValue, jsonObject, args);
            }
        },
        GETYEAR("getYear") {
            @Override
            public String handle(String eleValue, JSONObject  jsonObject ,String... args) {
                return ReturnValue.getYear(eleValue, args);
            }
        },
        SPLITANDSET("splitAndSet") {
            @Override
            public String handle(String eleValue, JSONObject jsonObject , String... args) {
                return ReturnValue.splitAndSet(eleValue, jsonObject,  args);
            }
        },
        MERGESTR("mergeStr") {
            @Override
            public String handle(String eleValue, JSONObject jsonObject , String... args) {
                return ReturnValue.mergeStr(eleValue, jsonObject,  args);
            }
        },
        ISCONTAINS("isContains") {
            @Override
            public String handle(String eleValue, JSONObject jsonObject, String... args) {
                return ReturnValue.isContains(eleValue, jsonObject, args);
            }
        };
        /**
         * 枚举属性
         */
        private final String methodName;

        flow(String methodName) {
            this.methodName = methodName;
        }

        public String getMethodName() {
            return methodName;
        }

        public abstract String handle(String eleValue, JSONObject jsonObject ,String... args)throws Exception;
    }
}
